package com.xajh.duoduo.eunm.position;

import com.xajh.duoduo.config.NumConfig;
import com.xajh.duoduo.environment.CurrentAccountPosition;
import com.xajh.duoduo.environment.CurrentRolePosition;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 账号位置
 */
public enum AccountPosition {
    ACCOUNT_0(125,85,"AccountPosition1"),
    ACCOUNT_1(85,140,"AccountPosition2"),
    ACCOUNT_2(60,210,"AccountPosition3"),
    ACCOUNT_3(60,280,"AccountPosition4"),

    ACCOUNT_4(85,340,"AccountPosition5"),
    ACCOUNT_5(775,80,"AccountPosition6"),
    ACCOUNT_6(730,140,"AccountPosition7"),
    ACCOUNT_7(710,210,"AccountPosition8"),

    ACCOUNT_8(710,280,"AccountPosition9"),
    ACCOUNT_9(735,340,"AccountPosition10"),
    ACCOUNT_10(1425,70,"AccountPosition11"),
    ACCOUNT_11(1380,125,"AccountPosition12"),

    ACCOUNT_12(1360,195,"AccountPosition13"),
    ACCOUNT_13(1360,265,"AccountPosition14"),
    ACCOUNT_14(1385,330,"AccountPosition15"),
    ACCOUNT_15(125,465,"AccountPosition16"),

    ACCOUNT_16(80,525,"AccountPosition17"),
    ACCOUNT_17(60,595,"AccountPosition18"),
    ACCOUNT_18(60,665,"AccountPosition19"),
    ACCOUNT_19(85,725,"AccountPosition20");


    private int x;
    private int y;
    private String comment;

    AccountPosition(int x, int y, String comment) {
        this.x = x;
        this.y = y;
        this.comment = comment;
    }

    public AccountPosition getNextAndSetCurrentAccountPosition(){
        Map<Integer, List<AccountPosition>> accountsGroup = CurrentAccountPosition.getAccountsGroup();
        Integer currentGroupIndex = CurrentAccountPosition.getGroupIndex();
        Integer currentItemIndex = CurrentAccountPosition.getItemIndex();

        int nextGroupIndex;
        int nextItemIndex;
        /**例：当前为1组2号位
         * 这个方法返回的应该是1组3号位
         * 但是，设置的currentAccount，应该是2组2号位
         * 因为当前账号需要移动到下方点击角色进入游戏，换号后需要从对应的2组2号位选下一个，即2组3号位
         * */
        //设置下一组的currentAccount
        //最后一组 且 不是最后一个
        if((currentGroupIndex + 1) == NumConfig.deviceNum && (currentItemIndex + 1) != NumConfig.accountNum){
            nextGroupIndex = 0;
            nextItemIndex = currentItemIndex + 1;
        }
        //最后一组 且 是最后一个
        else if((currentGroupIndex + 1) == NumConfig.deviceNum && (currentItemIndex + 1) == NumConfig.accountNum){
            nextGroupIndex = 0;
            nextItemIndex = 0;
        } else{
            nextGroupIndex = currentGroupIndex + 1;
            nextItemIndex = currentItemIndex;
        }

        //设置现在的位置
        CurrentAccountPosition.setCurrentAccountPosition(new AtomicReference<>(accountsGroup.get(nextGroupIndex).get(nextItemIndex)));
        CurrentAccountPosition.setGroupIndex(nextGroupIndex);
        CurrentAccountPosition.setItemIndex(nextItemIndex);

        //返回当前组的下一个Account来 进入游戏
        //如果当前组，已经是最后一个了。则返回null
        if((currentItemIndex + 1) == NumConfig.accountNum){
            return null;
        }
        return accountsGroup.get(currentGroupIndex).get(currentItemIndex + 1);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
}
